package com.gtt.lazy.controller;

import java.util.List;

import com.gtt.lazy.domain.Agent;
import com.gtt.lazy.domain.Category;
import com.gtt.lazy.domain.ColorCategory;
import com.gtt.lazy.domain.Colors;
import com.gtt.lazy.domain.Notice;
import com.gtt.lazy.domain.Sofa;
import com.gtt.lazy.service.AgentService;
import com.gtt.lazy.service.CategoryService;
import com.gtt.lazy.service.CategorySofaService;
import com.gtt.lazy.service.ColorCategoryService;
import com.gtt.lazy.service.ColorService;
import com.gtt.lazy.service.NoticeService;
import com.gtt.lazy.service.SofaService;

/**
 * 前台查看Action
 * 
 * @author 高甜甜
 * @date 2009-7-11
 */
public class ViewAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private CategoryService categoryService;
	private SofaService sofaService;
	private CategorySofaService categorySofaService;
	private AgentService agentService;
	private NoticeService noticeService;
	private ColorService colorService;
	private ColorCategoryService colorCategoryService;

	private int cid;// 沙发类别ID
	private int id;// 沙发ID
	private int ccid;// 色板类别ID
	private String uri;// 商品展示地址

	private Sofa sofa;

	private List<Category> cats;

	private List<Notice> notices;

	private List<Sofa> sofas;

	private List<Sofa> discountSofas;

	private List<Sofa> freshSofas;

	private List<Agent> agents;

	private List<Colors> colors;

	private List<ColorCategory> ccs;

	private String colorCatName;

	/**
	 * 首页
	 * 
	 * @return
	 */
	public String index() {
		cats = categoryService.list(0, 14);
		ccs = colorCategoryService.list();
		return SUCCESS;
	}

	/**
	 * 首页 - 内页
	 * 
	 * @return
	 */
	public String home() {
		notices = noticeService.list();
		agents = agentService.list(0, 4);
		discountSofas = sofaService.listDiscounted(0, 8);
		freshSofas = sofaService.listFresh(0, 8);
		return SUCCESS;
	}

	/**
	 * 所有类别列表
	 * 
	 * @return
	 */
	public String listCats() {
		// 确定所要显示的页数
		if (page == 0)
			page = 1;
		int start = (page - 1) * Constant.SIZE_VIEW_CATS;
		cats = categoryService.list(start, Constant.SIZE_VIEW_CATS);
		setPagination(categoryService.count(), Constant.SIZE_VIEW_CATS, page);
		return SUCCESS;
	}

	/**
	 * 某类别下所有沙发列表
	 * 
	 * @return
	 */
	public String listSofas() {
		uri = "listSofas.action";

		if (page == 0)
			page = 1;
		int start = (page - 1) * Constant.SIZE_VIEW_SOFAS;
		sofas = categorySofaService.listSofas(cid, start,
				Constant.SIZE_VIEW_SOFAS);
		setPagination(categorySofaService.countSofas(cid),
				Constant.SIZE_VIEW_CATS, page);
		return SUCCESS;
	}

	/**
	 * 所有特价商品列表
	 * 
	 * @return
	 */
	public String listDiscount() {
		uri = "listDiscount.action";

		if (page == 0)
			page = 1;
		int start = (page - 1) * Constant.SIZE_VIEW_SOFAS;
		sofas = sofaService.listDiscounted(start, Constant.SIZE_VIEW_SOFAS);
		setPagination(sofaService.countDiscounted(), Constant.SIZE_VIEW_CATS,
				page);
		return SUCCESS;
	}

	/**
	 * 所有新品列表
	 * 
	 * @return
	 */
	public String listFresh() {
		uri = "listFresh.action";

		if (page == 0)
			page = 1;
		int start = (page - 1) * Constant.SIZE_VIEW_SOFAS;
		sofas = sofaService.listFresh(start, Constant.SIZE_VIEW_SOFAS);
		setPagination(sofaService.countFresh(), Constant.SIZE_VIEW_CATS, page);

		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String listColorsView() {

		if (page == 0)
			page = 1;
		int start = (page - 1) * Constant.SIZE_VIEW_COLORS;

		colorCatName = "";
		if (ccid == 0) {
			ColorCategory colorCategory = colorCategoryService.list().get(0);
			ccid = colorCategory.getId();
			colorCatName = colorCategory.getName();
		} else {
			colorCatName = colorCategoryService.get(ccid).getName();
		}

		colors = colorService.list(ccid, start, Constant.SIZE_VIEW_COLORS);
		setPagination(colorService.count(ccid), Constant.SIZE_VIEW_COLORS, page);

		getSession().put("ccid", ccid);

		return SUCCESS;
	}

	/**
	 * 所有代理商列表
	 * 
	 * @return
	 */
	public String listAgents() {
		if (page == 0)
			page = 1;
		int start = (page - 1) * Constant.SIZE_VIEW_AGENTS;
		agents = agentService.list(start, Constant.SIZE_VIEW_AGENTS);
		setPagination(agentService.count(), Constant.SIZE_VIEW_AGENTS, page);
		return SUCCESS;
	}

	/**
	 * 展示沙发详细信息
	 * 
	 * @return
	 */
	public String getSofaDetail() {
		sofa = sofaService.get(id);
		return SUCCESS;
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public List<Agent> getAgents() {
		return agents;
	}

	public void setAgents(List<Agent> agents) {
		this.agents = agents;
	}

	public List<Category> getCats() {
		return cats;
	}

	public void setCats(List<Category> cats) {
		this.cats = cats;
	}

	public List<Colors> getColors() {
		return colors;
	}

	public void setColors(List<Colors> colors) {
		this.colors = colors;
	}

	public List<Sofa> getDiscountSofas() {
		return discountSofas;
	}

	public void setDiscountSofas(List<Sofa> discountSofas) {
		this.discountSofas = discountSofas;
	}

	public List<Sofa> getFreshSofas() {
		return freshSofas;
	}

	public void setFreshSofas(List<Sofa> freshSofas) {
		this.freshSofas = freshSofas;
	}

	public List<Notice> getNotices() {
		return notices;
	}

	public void setNotices(List<Notice> notices) {
		this.notices = notices;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public String getColorCatName() {
		return colorCatName;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public SofaService getSofaService() {
		return sofaService;
	}

	public void setSofaService(SofaService sofaService) {
		this.sofaService = sofaService;
	}

	public CategorySofaService getCategorySofaService() {
		return categorySofaService;
	}

	public void setCategorySofaService(CategorySofaService categorySofaService) {
		this.categorySofaService = categorySofaService;
	}

	public AgentService getAgentService() {
		return agentService;
	}

	public void setAgentService(AgentService agentService) {
		this.agentService = agentService;
	}

	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public int getCcid() {
		return ccid;
	}

	public void setCcid(int ccid) {
		this.ccid = ccid;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Sofa getSofa() {
		return sofa;
	}

	public void setSofa(Sofa sofa) {
		this.sofa = sofa;
	}

	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public List<ColorCategory> getCcs() {
		return ccs;
	}

	public List<Sofa> getSofas() {
		return sofas;
	}

	public void setSofas(List<Sofa> sofas) {
		this.sofas = sofas;
	}

	public ColorService getColorService() {
		return colorService;
	}

	public void setColorService(ColorService colorService) {
		this.colorService = colorService;
	}

	public ColorCategoryService getColorCategoryService() {
		return colorCategoryService;
	}

	public void setColorCategoryService(
			ColorCategoryService colorCategoryService) {
		this.colorCategoryService = colorCategoryService;
	}

}
